package com.scqkxx.gamebox.activity.myself;

import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

import com.android.tu.loadingdialog.LoadingDailog;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.MainActivity;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.loginutils.InputVerificationUtils;
import com.scqkxx.gamebox.activity.myself.loginutils.LoginRequestUtils;
import com.scqkxx.gamebox.activity.myself.loginutils.OnRequestListener;
import com.scqkxx.gamebox.activity.register.RegisterNameActivity;
import com.scqkxx.gamebox.databinding.ActivityLoginBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.event.LoginEvent;
import com.scqkzqtz.base.library.utils.DrawableUtil;
import com.scqkzqtz.base.library.utils.PreferenceUtils;
import com.scqkzqtz.base.library.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by zsx on 2018/8/14.
 */

public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    private LoadingDailog dialog;

    @Override
    public int setLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        initView();
    }

    public void initView() {
        binding.tvRegistered.setOnClickListener(v -> startActivity(new Intent(this, RegisterNameActivity.class)));
        binding.tvRight.setOnClickListener(v -> startActivity(new Intent(this, ChangePsdActivity.class)));
        binding.etPhoneNumber.setTag(InputVerificationUtils.TAG_PHONENUMBER);
        binding.etPassword.setTag(InputVerificationUtils.TAG_PASSWORD);
        InputVerificationUtils.psdShowHide(binding.checkboxPassword, binding.etPassword);
//        InputVerificationUtils.onPutListener(binding.tvLogin, binding.etPhoneNumber, binding.etPassword);
        binding.tvLogin.setOnClickListener(v -> {
            if (InputVerificationUtils.isPutMeet(binding.etPhoneNumber)) {
                LoginRequestUtils.loginByMobilePhone(LoginActivity.this, binding.etPhoneNumber.getText().toString().trim(),
                        binding.etPassword.getText().toString().trim(), new OnRequestListener() {
                            @Override
                            public void OnVerificationSuccess() {
                                binding.tvLogin.setEnabled(false);
                                LoadingDailog.Builder loadBuilder=new LoadingDailog.Builder(LoginActivity.this)
                                        .setMessage("加载中...")
                                        .setCancelable(true)
                                        .setCancelOutside(true);
                                dialog=loadBuilder.create();
                                dialog.show();
                            }

                            @Override
                            public void OnVerificationError(String s) {
                                binding.tvLogin.setEnabled(true);
                                ToastUtils.showShortToast(LoginActivity.this, s);
                            }

                            @Override
                            public void OnSuccer(Map<String, Object> map) {
                                Log.i("avUser",map+"");
                                if (dialog != null) dialog.dismiss();
                                binding.tvRegistered.setEnabled(true);
                                try {
//                                    Map<String, Object> mapData = (Map<String, Object>) map.get("data");
                                    String sessionToken = map.get("sessionToken") + "";
                                    AVUser.becomeWithSessionTokenInBackground(sessionToken, new LogInCallback<AVUser>() {
                                        @Override
                                        public void done(AVUser user, AVException e) {
                                            if (e == null) {
                                                PreferenceUtils.commitBoolean("isLogin"  , true);
                                                ActivityUtil.switchTo(LoginActivity.this,MainActivity.class);
                                                LoginActivity.this.finish();
                                            }
                                        }
                                    });
                                } catch (Exception e) {
                                    Log.i("avUser",e.getMessage());
                                    if (dialog != null) dialog.dismiss();
                                    binding.tvRegistered.setEnabled(true);   }
                            }

                            @Override
                            public void OnError(String s) {
                                if (dialog != null) dialog.dismiss();
                                binding.tvLogin.setEnabled(true);
                                ToastUtils.showShortToast(LoginActivity.this, s);
                            }

                            @Override
                            public void onSubscribe(Disposable d) {
                                addDisposable(d);
                            }
                        });
            } else {
                ToastUtils.showShortToast(LoginActivity.this, "邮箱输入错误，请稍后再试..");
            }
        });
    }
}
